package com.suoluo.zenadmin.controllers.mobile;

import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.country.CountryService;
import com.suoluo.zenadmin.service.dest.MobileService;
import com.suoluo.zenadmin.service.news.NewsService;
import com.suoluo.zenadmin.vo.news.News2VO;


@Path("")
public class MobileNewsController{
	// 注入的新闻service
	@Resource(name="newsService")
	private NewsService newsService;
	
	//手机版首页service类，新闻的只两个方法（查询、统计），所以并入这个service
	@Resource(name = "mobileService")
	private MobileService mobileService;
	
	@Resource(name="countryService")
	private CountryService countryService;
	
	/**
	 * 查看新闻详细页
	 * news.do?action=listNews&pageNum=1
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("news.do")
	public String get(Invocation inv,@Param("action") String action,@Param("pageNum") int pageNum) {
		if ("viewNews".equals(action)){
			return showNews(inv);
		}else {
			return listNews(inv, pageNum);
		}
	}
	
	/**
	 * 查看新闻详细页
	 * news.do?action=listNews&pageNum=1
	 * @param request
	 * @param response
	 * @return
	 */
	@Post("news.do")
	public String post(Invocation inv,@Param("action") String action,@Param("pageNum") int pageNum) {
		if ("viewNews".equals(action)){
			return showNews(inv);
		}else {
			return listNews(inv, pageNum);
		}
	}
	
	/**
	 * http://www.zentravel.cn/mobile/news.do?action=viewNews&newsId=49
	 * @return
	 */
	private String showNews(Invocation inv){
		int newsId = Integer.valueOf(inv.getParameter("newsId"));


		// 1.查询新闻的详细内容
		News2VO news = newsService.findNewsById(newsId);
		news.setContent(UBB.decode(news.getContent()));
		inv.addModel("news", news);
		inv.addModel("country",countryService.findCountryById(news.getCountryId()));
		
		// 2.查询新闻四个类似的新闻
		int pageSize = SystemConfigHolder.getRecommendSize();
		//int contentLength = SystemConfigHolder.getMobileContentLimit();
		
		List<News2VO> newsList = newsService.findSimilarNews(newsId, news.getFeatureCategoryId(), pageSize);
//		// UBB解码
//		for (News n : newsList) {
//			String newsSummary = n.getNewsSummary();
//			newsSummary = UBB.clear(newsSummary);
//			//UBB.decode(newsContent);
//			if(newsSummary.length() > contentLength)
//			{
//				n.setNewsSummary(newsSummary.substring(0, contentLength));
//			}
//			else
//			{
//				n.setNewsSummary(newsSummary);
//			}
//			
//		}
		inv.addModel("samilarNewsList", newsList);
		return "news/news";
	}

	/**
	 * 从导航页点击新闻进入新闻首页面
	 * news.do?action=listNews&pageNum=1
	 * @param request
	 * @param response
	 * @return
	 */
	private String listNews(Invocation inv,@Param("pageNum") int pageNum)
	{
		if (pageNum == 0){
			pageNum = 1;
		}
		//获取新闻列表信息
		List<News2VO> list = mobileService.findHomeNews(pageNum);
		//获取新闻总数
		int totalCount = newsService.findNewsCount();
		int pageRows = SystemConfigHolder.getMobilePageRows();
		int totalPages = PageUtil.calcPageCount(totalCount, pageRows);
		if (totalPages > pageNum){
			inv.addModel("nextPage",pageNum + 1);
		}else{
			inv.addModel("nextPage",0);
		}
		inv.addModel("newsList",list);
		
		return "news/listNews";
	}

}
